
package urnoted;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.*;

import urnoted.Controller;

public class StaffPanel extends JPanel {
    private static final int WIDTH = 400;
    private static final String IMAGE_PATH = "src/urnoted/images/trebleclef.png";
    private Controller controller;
    private ImageIcon clef;
    private Note note;
    private int width, height;  // size of rectangle
    private int x0, y0;         // location of rectangle
    private int n;              // number of possible notes: 12
    private ArrayList<StaffButton> buttons;

    public StaffPanel(Controller controller, Note note) {

    	setClefIcon();
        this.controller = controller;
        this.note = note;
        this.width = WIDTH;
        this.height = clef.getIconHeight();
        this.x0 = this.getX() + 10;
        this.y0 = this.getY() + 10;
        this.n = Note.values().length;
        this.setPreferredSize(new Dimension(WIDTH, clef.getIconHeight()));
        this.setBackground(Color.WHITE);
        
        this.buttons = new ArrayList<StaffButton>();
        this.setLayout(null);
        for (int i = n-1; i >= 0; i--) {
            StaffButton button = new StaffButton(controller, i,Note.values()[i].name());
            button.setBounds(100, 24 + 15*(n-i-1), 240, 15);
            buttons.add(button);
            this.add(button);
        }
    }
    
    public void setNote(Note n)
    {
    	note = n;
    }
    
    private void setClefIcon()
    {
    	ImageIcon base = new ImageIcon(IMAGE_PATH);
    	Image img = base.getImage();
    	BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_ARGB);
    	Graphics g = bi.createGraphics();
    	g.drawImage(img,0,0,(int)(base.getIconWidth() * .65),(int)(base.getIconHeight() * .65),null);
    	clef = new ImageIcon(bi);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        clef.paintIcon(this, g, 40, y0);
        drawStaff((Graphics2D)g);
        if(controller.getSkill().equals("Find Key for Note"))
        {
        	drawNote((Graphics2D)g);
        }
    }

    private void drawStaff(Graphics2D g2) {
        g2.setStroke(new BasicStroke(3));
        
        // Draw five horizontal staff lines:
        for (int i = 0; i < 5; i++) {
            int y = y0 + 30*i + 37;
            g2.drawLine(x0, y, width - x0, y);
        }
        
        // Draw the two vertical end lines:
        g2.drawLine(x0, y0 + 37, x0, y0 + 157);
        g2.drawLine(width - x0, y0 + 37, width - x0, y0 + 157);
        
        // Draw extra line for C4 and D4 notes:
        if (note == Note.C4) {
            int y = note.getY() + y0 + 15;
            g2.drawLine(x0 + 170, y, x0 + 250, y);
        }
    }

    private void drawNote(Graphics2D g2) {
        g2.setStroke(new BasicStroke(4));
        int y = y0 + note.getY();
        for (int i = 0; i < 6; i++) {
            g2.draw(new Ellipse2D.Double(x0 + 190+2*i, y+2, 40-4*i, 28));
        }
    }
}